S.Net = function (dom, option) {
    var me = this;
    Q.doSuperConstructor(me, S.Net, [dom]);
    S.setOption(me, me.option);
    S.setOption(me, option);
    me.stations = {};
    me.lines = {};
    me.listeners = {};
}

S.Net.prototype = {
    xtype: 'net',
    ownerCt: null, // 所属的容器
    option: {},

    setStations: function (stations, gOpt) {
        var me = this, i, len = stations.length;
        for (var i = 0; i < len; i++) {
            var option = stations[i], station = me.stations[option.id];
            S.setOption(option, gOpt);
            if (!station) {
                station = option.isTransformer ? new S.Node.Transformer(option) : new S.Node.Station(option);
                me.stations[option.id] = station;
                if (option.isTransformer && option.transfer && option.transfer != option.id) {
                    continue;
                }
                station.graph = me;
                me.graphModel.add(station);
            } else {
                station.setOption(option);
            }
        }
    },

    setLines: function (lines, gOpt) {
        var me = this, i, len = lines.length;
        for (var i = 0; i < len; i++) {
            var option = lines[i], line = me.lines[option.id];
            option = S.applyDepth(option, gOpt);
            if (!line) {
                line = new S.Node.Line(option);
                line.graph = me;
                me.lines[option.id] = line;
                me.graphModel.add(line);
            } else {
                line.setOption(option);
            }
            line.draw(line, line.isDouble);
        }
    },
    
    /**
     * 绘制所有断面
     */
    drawAllCuts : function(gOpt) {
		var me = this, lines = me.lines;
		for (var line in lines) {
			var stations = lines[line].stations, i, len = stations.length;
			for (var i = 0; i < len - 1; i++) {
				lines[line].setCuts([{
							stations : [stations[i], stations[i + 1]]
						}, {
							stations : [stations[i + 1], stations[i]]
						}], line, gOpt)
			}
		}
	},

    /**
     * @param {} options
     * @param {} isDouble
     * @param {} globalOpts	全局变量，格式如下
     * {
     * 		cutsRender:true/false,	初始化时是否绘制断面
     * 		lines:{},
     * 		stations:{},
     * 		cuts:{},
     * }
     */
    setOption: function (options, globalOpts) {
    	globalOpts = globalOpts ? globalOpts : {};
        var me = this;
        if (options.stations) {
            me.setStations(options.stations, globalOpts.stations);
            delete options.stations;
        }
        if (options.cuts) {
            me.cuts = options.cuts;
            delete options.cuts;
        }
        if (options.quadCuts) {
            me.quadCuts = options.quadCuts;
            delete options.quadCuts;
        }
        if (options.lines) {
            me.setLines(options.lines, globalOpts.lines);
            delete options.lines;
        }
        if(options.cuts || globalOpts.cutsRender){
        	me.drawAllCuts(globalOpts.cuts);
        }
        S.setOption(me, options);
        me.zoomToOverview();
    },

    setCrowdings: function (data) {
        var me = this, lines = me.graph.lines;
        data.forEach(function (e) {
            lines[e.line].setCrowdings(e.data);
        });
    },

    setCuts: function (data) {
        var me = this, lines = me.graph.lines;
        data.forEach(function (e) {
            lines[e.line].setCuts(e.data);
        });
    },
    
    /**
     * 设置断面拥挤度值
     * @param {} id		断面id
     * @param {} data	断面拥挤度值
     */
    setCutData : function(id, data) {
		var lines = this.lines;
		for (var i in lines) {
			var cut = lines[i].cuts && lines[i].cuts[id];
			if (cut) {
				cut.setData(data);
			}
		}
	}

};

Q.extend(S.Net, Q.Graph);

